/*分页类*/

class Page {

List totalList;
List itemList;
int totalPage;
int pageNo;
int nextPage;
int previousPage;
int firstPage;
int lastPage;
int pageSize;
String prefix;
int startRow;
int endRow;

public setPageNo(int page)
{
	initTotalPage();
	initPageNo(page)
	initStartRow()
	initEndRow()
	initNextPage()
	initPreviousPage()
	initFirstPage()
	initLastPage()
	initItemList()
	
	
}
public initPageNo(int page)

{
	if(page<1) this.pageNo=1;
	if(page>this.totalPage) this.pageNo=this.totalPage;
	this.pageNo=page;
}

public initNextPage()

{
	if(this.pageNo>=this.getTotalPage()) nextPage=0;
	else 
	{
		nextPage=this.pageNo+1;
	}
}

public initPreviousPage()

{
	if(this.pageNo<=1) previousPage=0;
	else
	{
		previousPage=this.pageNo-1
	}
}
public initFirstPage()
{
	firstPage= 1;	
}
public initLastPage()
{
	lastPage= this.getTotalPage();
}
public initItemList()

{
	this.itemList=totalList.subList(this.getStartRow(),this.getEndRow());
}

public initStartRow()
{
	startRow= (this.pageNo-1)*pageSize;
}

public initEndRow()
{
	endRow=this.pageNo*pageSize;
	if(endRow>this.totalList.size()) endRow=this.totalList.size();
}

public initTotalPage()
{
	int total=Math.round(totalList.size()/pageSize+0.49999);
	if(total<1)
		total=1;
	this.totalPage=total;
}

public getUrl(def pageNo)
{
	if(pageNo<=1)
		return prefix+".html";
	else
		return prefix+"-"+pageNo+".html";
}
	
  static void main(args) {
	  
	  List list=[];
	  
	  for(i in (1..1000))
	  {
		  list.add("element ${i}");
	  }
	  
	  int pageSize=10;
	  
	  Page page=new Page(totalList:list,pageSize:pageSize);
	  page.setPageNo(10);
	  println """
    page.startRow: ${page.startRow}	
    page.endRow: ${page.endRow}
	page.totalPage: ${page.totalPage}
	page.nextPage: ${page.nextPage}
	page.pageNo: ${page.pageNo}
	page.previousPage: ${page.previousPage};
	page.firstPage: ${page.firstPage};
	page.lastPage: ${page.lastPage};
	page.itemList.size(): ${page.itemList.size()};


	  """
    
  }

}